<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业全称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入企业全称" />
      </el-form-item>
      <el-form-item label="企业简称" prop="shortName">
        <el-input v-model="formData.shortName" placeholder="请输入企业简称" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input v-model="formData.legalPersonName" placeholder="请输入法人姓名" />
      </el-form-item>
      <el-form-item label="法人身份证号码" prop="legalPersonCard">
        <el-input v-model="formData.legalPersonCard" placeholder="请输入法人身份证号码" />
      </el-form-item>
    
      <el-form-item label="企业类型" prop="companyType">
        <el-radio-group v-model="formData.companyType">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMPANY_TYPE)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="企业LOGO" prop="logo">
        <UploadImg v-model="formData.logo" />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactPerson">
        <el-input v-model="formData.contactPerson" placeholder="请输入联系人姓名" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="formData.contactPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="联系人职位" prop="contactPosition">
        <el-input v-model="formData.contactPosition" placeholder="请输入联系人职位" />
      </el-form-item>
      <!-- <el-form-item label="行政区划" prop="regionCode">
        <el-input v-model="formData.regionCode" placeholder="请输入行政区划" />
      </el-form-item> -->
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="营业执照" prop="businessLicense">
        <el-input v-model="formData.businessLicense" placeholder="请输入营业执照" />
      </el-form-item>
      <el-form-item label="营业执照图片" prop="businessLicenseImage">
        <UploadImg v-model="formData.businessLicenseImage" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="渠道编码" prop="channelCode">
        <el-input v-model="formData.channelCode" placeholder="请输入渠道编码" />
      </el-form-item>
      <el-form-item label="生效日期" prop="effectDate">
        <el-date-picker
          v-model="formData.effectDate"
          type="date"
          value-format="x"
          placeholder="选择生效日期"
        />
      </el-form-item>
      <el-form-item label="失效时间" prop="expireDate">
        <el-date-picker
          v-model="formData.expireDate"
          type="date"
          value-format="x"
          placeholder="选择失效时间"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { CompanyApi, CompanyVO } from '@/api/member/company'

/** 企业表（总部/区域合伙人/物业公司/小区物业） 表单 */
defineOptions({ name: 'CompanyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  shortName: undefined,
  legalPersonName: undefined,
  legalPersonCard: undefined,
  parentId: undefined,
  companyType: undefined,
  logo: undefined,
  contactPerson: undefined,
  contactPhone: undefined,
  contactPosition: undefined,
  regionCode: undefined,
  address: undefined,
  businessLicense: undefined,
  businessLicenseImage: undefined,
  status: undefined,
  channelCode: undefined,
  effectDate: undefined,
  expireDate: undefined,
  remark: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '企业全称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CompanyApi.getCompanyById(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CompanyVO
    if (formType.value === 'create') {
      await CompanyApi.createCompany(data)
      message.success(t('common.createSuccess'))
    } else {
      await CompanyApi.updateCompany(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    shortName: undefined,
    legalPersonName: undefined,
    legalPersonCard: undefined,
    parentId: undefined,
    companyType: undefined,
    logo: undefined,
    contactPerson: undefined,
    contactPhone: undefined,
    contactPosition: undefined,
    regionCode: undefined,
    address: undefined,
    businessLicense: undefined,
    businessLicenseImage: undefined,
    status: undefined,
    channelCode: undefined,
    effectDate: undefined,
    expireDate: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>